import 'package:demo/constants/constants.dart';
import 'package:demo/model/index_cell.dart';
import 'package:demo/util/data_utils.dart';
import 'package:demo/widgets/index_list_cell.dart';
import 'package:demo/widgets/index_list_header.dart';
import 'package:flutter/material.dart';

const pageIndexArray = Constants.RANK_BEFORE;

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  List<IndexCell> _listData = new List();
  int _pageIndex = 0;
  Map<String, dynamic> _params = {"src": 'web', "category": "all", "limit": 20};
  bool _isLogin = false;

  @override
  void initState() {
    super.initState();
    getList(false);
  }

  _renderList(context, index) {
    if(index == 0) {
      return IndexListHeader(false);
    }
    return IndexListCell(cellInfo: _listData[index-1]);
  }

  @override
  Widget build(BuildContext context) {
      if(_listData.length == 0) {
        return Center(
          child: CircularProgressIndicator()
        );
      }
      return ListView.builder(
        itemCount: _listData.length + 1, //添加一个header
        itemBuilder: (context, index) => _renderList(context, index),
      );
  }


  getList(bool isLoadMore) {
    if(!isLoadMore){
      // reload的时候重置page
      _pageIndex = 0;
    }
    /*_params['before'] = pageIndexArray[_pageIndex];
    DataUtils.getIndexListData(_params).then((result) {
      setState(() {
        _listData = result;
      });
    });*/
    DataUtils.getIndexListData(_params).then((resultList) {
      setState(() {
        _listData = resultList;
      });
    });
  }
}

